package Chapter05.cak;

import java.util.Scanner;

public class Gobang {
    private static final int BOARD_SIZE = 15;
    private static final char EMPTY = '-';
    private static final char BLACK = 'X';
    private static final char WHITE = 'O';

    private static char[][] board = new char[BOARD_SIZE][BOARD_SIZE];

    private static boolean isBlackTurn = true;

    public static
    void main(String[] args) {
        initBoard();
        printBoard();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请输入落子位置（格式：行,列，如3,4）：");
            String input = scanner.nextLine();
            String[] positions = input.split(",");
            if (positions.length != 2) {
                System.out.println("输入格式错误，请重新输入！");
                continue;
            }
            int row = Integer.parseInt(positions[0]) - 1;
            int col = Integer.parseInt(positions[1]) - 1;
            if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE || board[row][col] != EMPTY) {
                System.out.println("输入位置非法，请重新输入！");
                continue;
            }
            board[row][col] = isBlackTurn ? BLACK : WHITE;
            printBoard();
            if (checkWin(row, col)) {
                System.out.println((isBlackTurn ? "黑方" : "白方") + "获胜！");
                break;
            }
            if (checkDraw()) {
                System.out.println("平局！");
                break;
            }
            isBlackTurn = !isBlackTurn;
        }
    }

    private static
    void initBoard() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = EMPTY;
            }
        }
    }

    private static
    void printBoard() {
        System.out.print(" ");
        for (int i = 0; i < BOARD_SIZE; i++) {
            System.out.print(i + 1);
        }
        System.out.println();
        for (int i = 0; i < BOARD_SIZE; i++) {
            System.out.print(i + 1);
            for (int j = 0; j < BOARD_SIZE; j++) {
                System.out.print(board[i][j]);
            }
            System.out.println();
        }
    }

    private static
    boolean checkWin(int row, int col) {
        char color = isBlackTurn ? BLACK : WHITE;
        int count = 1;
        for (int i = row - 1; i >= 0; i--) {
            if (board[i][col] == color) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row + 1; i < BOARD_SIZE; i++) {
            if (board[i][col] == color) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        for (int j = col - 1; j >= 0; j--) {
            if (board[row][j] == color) {
                count++;
            } else {
                break;
            }
        }
        for (int j = col + 1; j < BOARD_SIZE; j++) {
            if (board[row][j] == color) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (board[i][j] == color) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row + 1, j = col + 1; i < BOARD_SIZE && j < BOARD_SIZE; i++, j++) {
            if (board[i][j] == color) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }
        count = 1;
        for (int i = row - 1, j = col + 1; i >= 0 && j < BOARD_SIZE; i--, j++) {
            if (board[i][j] == color) {
                count++;
            } else {
                break;
            }
        }
        for (int i = row + 1, j = col - 1; i < BOARD_SIZE && j >= 0; i++, j--) {
            if (board[i][j] == color) {
                count++;
            } else {
                break;
            }
        }
        if (count >= 5) {
            return true;
        }
        return false;
    }

    private static
    boolean checkDraw() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                if (board[i][j] == EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }
}